using AutoMapper;
using Common.Framework.Core.Common;
using VehicleModule.Api.Model;
using VehicleModule.Api.Repository;
using Microsoft.AspNetCore.Http;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Common.Framework.Core.Extension;
	
namespace VehicleModule.Api.Service
{
    /// <summary>
    ///车队表 Service
    /// </summary>
    public class VehicleteamService : BaseService, IVehicleteamService
    {
        private readonly IMapper mapper;
        private readonly IVehicleteamRepository vehicleteamRepository;
	
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="vehicleteamRepository"></param>
        /// <param name="httpContextAccessor"></param>
        public VehicleteamService(
            IMapper mapper
            , IVehicleteamRepository vehicleteamRepository
            , IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
        {
            this.mapper = mapper;
            this.vehicleteamRepository = vehicleteamRepository;
        }
	
	
        #region 增
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ServiceResult> CreateAsync(VehicleteamCreateRequest request)
        {
            ServiceResult result = new ServiceResult();
            Vehicleteam vehicleteam = mapper.Map<Vehicleteam>(request);
            var insertResult = await vehicleteamRepository.InsertAsync(vehicleteam);
            if (insertResult.Id > 0)
            {
                result.Code = StatusCodes.Status200OK;
                result.Msg = "操作成功";
            }
            else
            {
                result.Code = StatusCodes.Status400BadRequest;
                result.Msg = "操作失败";
            }
            return result;
        }
        #endregion
	
        #region 删
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ServiceResult> DeleteAsync(IdRequest request)
        {
            ServiceResult result = new ServiceResult();
            var deleteResult = await vehicleteamRepository.SoftDeleteByIdAsync(request.Id);
            if (deleteResult)
            {
                result.Code = StatusCodes.Status200OK;
                result.Msg = "操作成功";
            }
            else
            {
                result.Code = StatusCodes.Status400BadRequest;
                result.Msg = "操作失败";
            }
            return result;
        }
        #endregion
	
        #region 改
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ServiceResult> UpdateAsync(VehicleteamUpdateRequest request)
        {
            ServiceResult result = new ServiceResult();
            Vehicleteam vehicleteam = mapper.Map<Vehicleteam>(request);
            var updateResult = await vehicleteamRepository.UpdateByIdAsync(vehicleteam, request.Id);
            if (updateResult)
            {
                result.Code = StatusCodes.Status200OK;
                result.Msg = "操作成功";
            }
            else
            {
                result.Code = StatusCodes.Status400BadRequest;
                result.Msg = "操作失败";
            }
            return result;
        }
        #endregion
	
        #region 查
        /// <summary>
        /// 查询 单条记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ServiceResult> GetAsync(IdRequest request)
        {
            ServiceResult result = new ServiceResult();
            var queryResult = await vehicleteamRepository.QueryByIdAsync(request.Id);
            if (queryResult != null)
            {
                result.Code = StatusCodes.Status200OK;
                result.Data = queryResult;
            }
            else
            {
                result.Code = StatusCodes.Status200OK;
                result.Msg = "暂无数据";
            }
            return result;
        }
        /// <summary>
        /// 查询 分页记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ServiceResult<VehicleteamGetListResponse>> GetListAsync(VehicleteamGetListRequest request)
        {
            ServiceResult <VehicleteamGetListResponse> result = new ServiceResult<VehicleteamGetListResponse>();
            Expression<Func<Vehicleteam, bool>> whereExpression = GetListQueryWhere(request);
            var queryResult = await vehicleteamRepository.QueryPageListByExpressionAsync(whereExpression, request.PageIndex, request.PageSize);
            if (queryResult.Total > 0)
            {
                result.Code = StatusCodes.Status200OK;
                result.Data = new VehicleteamGetListResponse
                {
                    Total = queryResult.Total,
                    DataList = queryResult.Data
                };
            }
            else
            {
                result.Code = StatusCodes.Status200OK;
                result.Msg = "暂无数据";
                result.Data = new VehicleteamGetListResponse();
            }
            return result;
        }
        private static Expression<Func<Vehicleteam, bool>> GetListQueryWhere(VehicleteamGetListRequest request)
        {
            Expression<Func<Vehicleteam, bool>> whereExpression = s => true;
            return whereExpression;
        }

        #endregion
    }
}
